#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn],cnt[maxn],kp[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    LL n,k;cin>>n>>k;
    for(LL i=1;i<=n;i++) cin>>a[i],cnt[i]=0,kp[i]=0;
    for(LL i=0;i<n;i++) cnt[i]=a[i+1]-a[i],kp[i]=a[i+1]-a[i];
    LL ans=0;
    for(LL i=0;i<n;i++){
        if(cnt[i]!=0){
            ans+=abs(cnt[i]);
            if(cnt[i]>0){
                if(i+k>n){
                    break;
                }
                cnt[i+k]-=cnt[i];
                cnt[i]=0;
            }
            else if(cnt[i]<0){
                if(i+k>n){
                    break;
                }
                cnt[i+k]+=cnt[i];
                cnt[i]=0;
            }
        }
    }
    LL res=0;
    for(LL i=n-1;i>=0;i--){
        if(kp[i]!=0){
            res+=abs(kp[i]);
            if(kp[i]>0){
                if(i-k<0) break;
                kp[i-k]-=kp[i];
                kp[i]=0;
            }
            else if(kp[i]<0){
                if(i-k<0) break;
                kp[i-k]=0;
            }
        }
    }

    bool flag1=1;bool flag2=1;
    for(LL i=1;i<n;i++){
        if(cnt[i]!=0){
            flag1=0;
        }
        if(kp[i]!=0){
            flag2=0;
        }
    }
    if(0==flag1&&0==flag2){
        cout<<"-1"<<"\n";
    }
    else cout<<min(ans,res)<<"\n";
  }
return 0;
}
